layui.use(['jquery'], function () {
    var $ = layui.jquery;
    article.Init($);//初始化共用js
   
});
var article = {};
var currentPage = 0;
var totalPage = 0;
article.Init = function ($) {
    //var $ = layui.jquery,
    var localObj = window.location;
    var contextPath = localObj.pathname.split("/")[1];
    currentPage = 1
    getNotes();
    blogtype();
    $('#all').click(function (){
        location.reload(true);
    })
    //类别导航开关点击事件
    $('.category-toggle').click(function (e) {
        e.stopPropagation();    //阻止事件冒泡
        categroyIn();
    });
    //网页触底事件
    $(window).scroll(function(){
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if((Math.abs(scrollTop + windowHeight-scrollHeight)<1)&currentPage<=totalPage){
            $.ajax({
                type:"get",
                url:"/cloud_note/allNotes",
                data:{"currentPage":currentPage},
                dataType: "json",
                success:function(result){
                    var notes = result.page.notes;
                    currentPage = currentPage+1;
                    for (let i = 0;  i < notes.length; i++) {
                        $("#LAY_bloglist").append("<section class=\"article-item zoomIn article\">" +
                            "<h5 class=\"title\">" +
                            "<span class=\"fc-blue\">【原创】</span>" +
                            "<a href='/cloud_note/note/read.html?noteId="+notes[i].noteId+"'>"+ notes[i].title +"</a>" +
                            "</h5>" +
                            "<div class=\"time\">" +
                            "<span class=\"day\">"+getYMDHMS(notes[i].pubTime).day+"</span>" +
                            "\<span class=\"month fs-18\">"+getYMDHMS(notes[i].pubTime).month+"<span class=\"fs-14\">月</span></span>" +
                            "<span class=\"year fs-18 ml10\">"+getYMDHMS(notes[i].pubTime).year+"</span>" +
                            "</div>"+
                            "<div class=\"read-more\">" +
                            "</div>" +
                            "<aside class=\"f-oh footer\">" +
                            "<div class=\"f-fl tags\">" +
                            "<span class=\"fa fa-tags fs-16\"></span>" +
                            "<a class=\"tag\">"+notes[i].typename+"</a>" +
                            "</div>       " +
                            "<div class=\"f-fr\">           " +
                            "<span class=\"read\">               " +
                            "<i class=\"fa fa-eye fs-16\"></i>               " +
                            "<i class=\"num\">"+notes[i].visits+"</i>           " +
                            "</span>           " +
                            "<span class=\"ml20\">               " +
                            "<i class=\"fa fa-comments fs-16\"></i>               " +
                            "<a href=\"javascript:void(0)\" class=\"num fc-grey\">1</a>           " +
                            "</span>       " +
                            "</div>   " +
                            "</aside>" +
                            "</section>")
                    }
                    if(currentPage==result.page.totalPage+1){
                        $("#LAY_bloglist").append('<div class="layui-flow-more">没有更多了</div>')
                    }
                }

            })
        }
    });
    //类别导航点击事件，用来关闭类别导航
    $('.article-category').click(function () {
        categoryOut();
    });
    //遮罩点击事件
    $('.blog-mask').click(function () {
        categoryOut();
    });
    $('.f-qq').on('click', function () {
        window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + $(this).attr("href") + '&sharesource=qzone&title=' + $(this).attr("title") + '&pics=' + $(this).attr("cover") + '&summary=' + $(this).attr("desc") + '&desc=你的分享简述' + $(this).attr("desc"));
    });
    $("body").delegate(".fa-times", "click", function () {
        $(".search-result").hide().empty(); $("#searchtxt").val("");
        $(".search-icon i").removeClass("fa-times").addClass("fa-search");
    });
    //加载Note/分类导航模块
    function getNotes(){
        $.ajax({
            type:"get",
            url:"/cloud_note/allNotes",
            data:{"currentPage":currentPage},
            dataType: "json",
            success:function(result){
                var notes = result.page.notes;
                var types = result.typeList;
                totalPage = result.page.totalPage;
                currentPage = currentPage+1;
                for (let i = 0; i < types.length; i++) {
                    $('#category').append('<li value="'+types[i].typeid+'"><a class="type">'+types[i].typename+'</a></li>')
                }
                $('#category').find('a').click(function(){
                    var typeid = $(this).parent().val()
                    $.ajax({
                        type:"get",
                        url:"/cloud_note/getNoteByType",
                        data:{"typeid":typeid},
                        dataType:"json",
                        success:function (result){
                            $ (window).unbind ('scroll');
                            $("#LAY_bloglist").empty()
                            for (let i = 0;  i < result.length; i++) {
                                $("#LAY_bloglist").append("<section class=\"article-item zoomIn article\">" +
                                    "<h5 class=\"title\">" +
                                    "<span class=\"fc-blue\">【原创】</span>" +
                                    "<a href='/cloud_note/note/read.html?noteId="+result[i].noteId+"'>"+ result[i].title +"</a>" +
                                    "</h5>" +
                                    "<div class=\"time\">" +
                                    "<span class=\"day\">"+getYMDHMS(result[i].pubTime).day+"</span>" +
                                    "\<span class=\"month fs-18\">"+getYMDHMS(result[i].pubTime).month+"<span class=\"fs-14\">月</span></span>" +
                                    "<span class=\"year fs-18 ml10\">"+getYMDHMS(result[i].pubTime).year+"</span>" +
                                    "</div>"+
                                    "<div class=\"read-more\">" +
                                    "</div>" +
                                    "<aside class=\"f-oh footer\">" +
                                    "<div class=\"f-fl tags\">" +
                                    "<span class=\"fa fa-tags fs-16\"></span>" +
                                    "<a class=\"tag\">"+result[i].typename+"</a>" +
                                    "</div>       " +
                                    "<div class=\"f-fr\">           " +
                                    "<span class=\"read\">               " +
                                    "<i class=\"fa fa-eye fs-16\"></i>               " +
                                    "<i class=\"num\">"+result[i].visits+"</i>           " +
                                    "</span>           " +
                                    "<span class=\"ml20\">               " +
                                    "<i class=\"fa fa-comments fs-16\"></i>               " +
                                    "<a href=\"javascript:void(0)\" class=\"num fc-grey\">1</a>           " +
                                    "</span>       " +
                                    "</div>   " +
                                    "</aside>" +
                                    "</section>")
                            }
                        }
                    })
                })
                for (let i = 0;  i < notes.length; i++) {
                    $("#LAY_bloglist").append("<section class=\"article-item zoomIn article\">" +
                        "<h5 class=\"title\">" +
                        "<span class=\"fc-blue\">【原创】</span>" +
                        "<a href='/cloud_note/note/read.html?noteId="+notes[i].noteId+"'>"+ notes[i].title +"</a>" +
                        "</h5>" +
                        "<div class=\"time\">" +
                        "<span class=\"day\">"+getYMDHMS(notes[i].pubTime).day+"</span>" +
                        "\<span class=\"month fs-18\">"+getYMDHMS(notes[i].pubTime).month+"<span class=\"fs-14\">月</span></span>" +
                        "<span class=\"year fs-18 ml10\">"+getYMDHMS(notes[i].pubTime).year+"</span>" +
                        "</div>"+
                        "<div class=\"read-more\">" +
                        "</div>" +
                        "<aside class=\"f-oh footer\">" +
                        "<div class=\"f-fl tags\">" +
                        "<span class=\"fa fa-tags fs-16\"></span>" +
                        "<a class=\"tag\">"+notes[i].typename+"</a>" +
                        "</div>       " +
                        "<div class=\"f-fr\">           " +
                        "<span class=\"read\">               " +
                        "<i class=\"fa fa-eye fs-16\"></i>               " +
                        "<i class=\"num\">"+notes[i].visits+"</i>           " +
                        "</span>           " +
                        "<span class=\"ml20\">               " +
                        "<i class=\"fa fa-comments fs-16\"></i>               " +
                        "<a href=\"javascript:void(0)\" class=\"num fc-grey\">1</a>           " +
                        "</span>       " +
                        "</div>   " +
                        "</aside>" +
                        "</section>")
                }
            }

        })
    }
    //模糊搜索
    $('#searchtxt').bind('input propertychange',function (){
        $('.search-result').empty()
        var t = $('#searchtxt').val()
        if(t==""){
            $('.search-result').css('display','none')
        }
        else{
            $('.search-result').css('display','block');
            $.ajax({
                type:"get",
                url:"/cloud_note/fuzzyQ",
                data: {"title":t},
                dataType: "json",
                success:function (result) {
                    for (let i = 0; i < result.length; i++) {
                        $('.search-result').append('<li class="child"><a class="findKey" href="/cloud_note/note/read.html?noteId='+result[i].noteId+'">'+result[i].title+'</a></li>');
                    }
                }
            })
        }

    })

    //时间戳转为标准时间
    function getYMDHMS(a) {
        var time = new Date(a)
        var year = time.getFullYear(),
            month = time.getMonth() + 1,
            day = time.getDate(),
            hours = time.getHours(),
            minute = time.getMinutes(),
            second = time.getSeconds();

        if (month < 10) { month = '0' + month; }
        if (day < 10) { day = '0' + day; }
        if (hours < 10) { hours = '0' + hours; }
        if (minute < 10) { minute = '0' + minute; }
        if (second < 10) { second = '0' + second; }

        return {
            year: year,
            month: month,
            day: day,
            hours: hours,
            minute: minute,
            second: second,
            date: year+"-"+month+"-"+day+" "+hours+':'+minute+':'+second
        }
    }
    //显示类别导航
    function categroyIn() {
        $('.category-toggle').addClass('layui-hide');
        $('.article-category').unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');
        $('.blog-mask').unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');
        $('.blog-mask').removeClass('maskOut').addClass('maskIn');
        $('.blog-mask').removeClass('layui-hide').addClass('layui-show');
        $('.article-category').removeClass('categoryOut').addClass('categoryIn');
        $('.article-category').addClass('layui-show');
    }
    //根据类型获取文章

    /**
     * 将关键字变为红色
     * @param keyWord   关键字字符串
     * @returns .finKey class类为finKey
     */
    //隐藏类别导航
    function categoryOut() {
        $('.blog-mask').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            $('.blog-mask').addClass('layui-hide');
        });
        $('.article-category').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            $('.article-category').removeClass('layui-show');
            $('.category-toggle').removeClass('layui-hide');
        });
        $('.blog-mask').removeClass('maskIn').addClass('maskOut').removeClass('layui-show');
        $('.article-category').removeClass('categoryIn').addClass('categoryOut');
    }
    function blogtype() {
        $('#category li').hover(function () {
            $(this).addClass('current');
            var num = $(this).attr('data-index');
            $('.slider').css({ 'top': ((parseInt(num) - 1) * 40) + 'px' });
        }, function () {
            $(this).removeClass('current');
            $('.slider').css({ 'top': slider });
        });
        $(window).scroll(function (event) {
            var winPos = $(window).scrollTop();
            if (winPos > 750)
                $('#categoryandsearch').addClass('fixed');
            else
                $('#categoryandsearch').removeClass('fixed');
        });
    };
};

